Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data

Namespace Bring2mind.DNN.Modules.Guestbook.Data

 Public Class SqlDataProvider
  Inherits DataProvider

#Region "Private Members"

  Private Const ProviderType As String = "data"

  Private _providerConfiguration As DotNetNuke.Framework.Providers.ProviderConfiguration = DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)
  Private _connectionString As String
  Private _providerPath As String
  Private _objectQualifier As String
  Private _databaseOwner As String

#End Region

#Region "Constructors"

  Public Sub New()

   ' Read the configuration specific information for this provider
   Dim objProvider As DotNetNuke.Framework.Providers.Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), DotNetNuke.Framework.Providers.Provider)

   ' Read the attributes for this provider
   If objProvider.Attributes("connectionStringName") <> "" AndAlso _
   System.Configuration.ConfigurationSettings.AppSettings(objProvider.Attributes("connectionStringName")) <> "" Then
    _connectionString = System.Configuration.ConfigurationSettings.AppSettings(objProvider.Attributes("connectionStringName"))
   Else
    _connectionString = objProvider.Attributes("connectionString")
   End If

   _providerPath = objProvider.Attributes("providerPath")

   _objectQualifier = objProvider.Attributes("objectQualifier")
   If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
    _objectQualifier += "_"
   End If

   _databaseOwner = objProvider.Attributes("databaseOwner")
   If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
    _databaseOwner += "."
   End If

  End Sub

#End Region

#Region "Properties"

  Public ReadOnly Property ConnectionString() As String
   Get
    Return _connectionString
   End Get
  End Property

  Public ReadOnly Property ProviderPath() As String
   Get
    Return _providerPath
   End Get
  End Property

  Public ReadOnly Property ObjectQualifier() As String
   Get
    Return _objectQualifier
   End Get
  End Property

  Public ReadOnly Property DatabaseOwner() As String
   Get
    Return _databaseOwner
   End Get
  End Property

#End Region


#Region "Entries Methods"

	Public Overrides Function GetEntries(ByVal EntryId As Integer, ByVal ModuleId as Integer) As IDataReader
		Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "GBook_GetEntries", EntryId, ModuleId), IDataReader)
	End Function
	
	Public Overrides Function AddEntries(ByVal Body As String, ByVal DateSubmitted As Date, ByVal Email As String, ByVal IsApproved As Boolean, ByVal ModuleId As Integer, ByVal Picture As String, ByVal UserName As String) As Integer
		Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "GBook_AddEntries", Body, DateSubmitted, Email, IsApproved, ModuleId, Picture, UserName), Integer)
	End Function
	
	Public Overrides Sub UpdateEntries(ByVal EntryId As Integer, ByVal Body As String, ByVal DateSubmitted As Date, ByVal Email As String, ByVal IsApproved As Boolean, ByVal ModuleId As Integer, ByVal Picture As String, ByVal UserName As String)
		SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "GBook_UpdateEntries", EntryId, Body, DateSubmitted, Email, IsApproved, ModuleId, Picture, UserName)
	End Sub
	
	Public Overrides Sub DeleteEntries(ByVal EntryId As Integer)
		SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "GBook_DeleteEntries", EntryId)
	End Sub
	
	Public Overrides Function GetEntriesByModules(ByVal moduleID As Integer , IncludeNonApproved As Boolean) As IDataReader
		Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "GBook_GetEntriesByModules", moduleID, IncludeNonApproved), IDataReader)
	End Function

#End Region

#Region "Other Procedures"
#End Region

 End Class

End Namespace
